package com.candy.localaccount;

import android.accounts.AccountManager;
import android.accounts.NetworkErrorException;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class CandyAuthenticateService extends Service {
    private final String TAG = "AuthenticateService";
    private CandyAuthenticator mAuthenticator;

    @Override
    public void onCreate() {
        Log.d(TAG, "service onCreate!");
        mAuthenticator = new CandyAuthenticator(this);
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "Service bind. Action: " + intent.getAction());
        if (AccountManager.ACTION_AUTHENTICATOR_INTENT.equals(intent.getAction())) {
            return mAuthenticator.getIBinder();
        } else {
            return null;
        }
    }

    @Override
    public void onStart(Intent intent, int startId) {
        Log.d(TAG, "service onStart!");
        if (intent == null) {
            Log.d(TAG, "service onStart! intent is null");
            return;
        }
        try {
            mAuthenticator.addAccount(null, null, null, null, null);
        } catch (NetworkErrorException e) {
            Log.d(TAG, "add account exception" + e);
        }
    }
}
